package com.base.chat.presenter;

import android.app.Activity;

import com.base.chat.bean.NewFriendsBean;
import com.base.chat.bean.SearchFriendsBean;
import com.base.chat.view.NewFriendsListView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by zw
 */

public class NewFriendsListPresenter extends BasePresenter<NewFriendsListView>{
    public NewFriendsListPresenter(NewFriendsListView view, Activity activity) {
        super(view, activity);
    }

    public void getList(final Integer page,final  Integer limit){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        Disposable enqueue = RealNetWorkUtil.get().method("api-im/friend/searchFriendNotices").params(map)
                .build().enqueue(NewFriendsBean.class, new CallBackImpl<NewFriendsBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                        mView.returnError();
                    }

                    @Override
                    public void onNext(NewFriendsBean list) {
                        mView.hidePb();
                          if (page == 1){
                              //刷新
                              if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                  mView.returnList(list);
                              }else {
                                  mView.returnNoData();
                              }
                          }else {
                              //更多
                              if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                  mView.returnMoreList(list);
                              }else {
                                  mView.returnNoMoreData();
                              }
                          }

                    }
                });

        mCompositeDisposable.add(enqueue);
    }

    /**
     * 同意/拒绝
     * @param id
     * @param handleType
     */
    public void agreeFriend(String id,String handleType){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("friendApplyId",id);
        map.put("handleType",handleType);
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-im/friend/handleFriendRecord").content(map)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                        mView.returnHandleRefuseFriendError("出错了");
                    }

                    @Override
                    public void onNext(String msg) {
                        mView.hidePb();
                        mView.returnHandleAgreeFriendSuccess("操作成功");

                    }
                });

        mCompositeDisposable.add(enqueue);
    }
}
